package org.dreamland.infrastructure.security

import org.dreamland.infrastructure.persistence.mapper.UserMapper
import org.springframework.security.core.userdetails.User
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Component

@Component
class DaoUserDetailService(private val userMapper: UserMapper): UserDetailsService {
    override fun loadUserByUsername(username: String?): UserDetails? {
        if(username == null)
            return null
        val user = userMapper.queryUserByName(username) ?: throw UsernameNotFoundException("用户名或者密码错误")
        return User
            .withUsername(user.username)
            .password(user.password)
            .build()
    }
}